<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996 15899929162
// +----------------------------------------------------------------------
namespace app\common\dao\contract;

use app\common\dao\BaseDao;
use app\common\model\contract\ContractLesseeModel;

class ContractLesseeDao extends BaseDao
{

    /**
     * @return ContractLesseeModel
     */
    protected function getModel(): string
    {
        return ContractLesseeModel::class;
    }

    public function search(array $where, int $propertyId = null)
    {
        return $this->getModel()::getDB()
            ->when($propertyId !== null, function ($query) use ($propertyId) {
                $query->where('property_id', $propertyId);
            })
            ->when(isset($where['contract_id']) && $where['contract_id'] !== '', function ($query) use ($where) {
                $query->where('contract_id', intval($where['contract_id']));
            })
            ->when(isset($where['is_type']) && $where['is_type'] !== '', function ($query) use ($where) {
                $query->where('is_type', intval($where['is_type']));
            })
            ->when(isset($where['is_owner']) && $where['is_owner'] !== '', function ($query) use ($where) {
                $query->where('is_owner', intval($where['is_owner']));
            })
            ->when(isset($where['keywords']) && $where['keywords'] !== '', function ($query) use ($where) {
                $query->whereLike('username|number|remark', '%' . trim($where['keywords']) . '%');
            });
    }

    /**
     * 根据租赁ID退租
     * @param int $contractId
     * @return \app\common\model\BaseModel
     *
     * @author Amos
     * 2024-06-13 22:22
     */
    public function throwALeaseByContractId(int $contractId)
    {
        return $this->getSearch([])
            ->where('contract_id', $contractId)
            ->update([
                'status' => 2
            ]);
    }

    /**
     * 根据租赁ID删除住户信息
     * @param int $id
     * @return bool
     *
     * @author Amos
     * 2024-06-13 22:22
     */
    public function deleteByContractId(int $id)
    {
        return $this->getSearch([
            'contract_id' => $id
        ])->delete();
    }

}
